﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using ALPackage;

namespace HYOK
{
    //GameSetting序列化数据
    [System.Serializable]
    public class GameSettingData
    {
        //当前语言
        public EGameLanguage eLang = EGameLanguage.EN_US;
        //是否是第一次进入游戏
        public bool bIsFirstEnterGame = true;
        // 是否使用单位高清资源
        public bool bUsingLowActorClarity = true;
        
        //是否使用音效
        public bool bUsingAudio = true;
        //是否高帧率
        public bool bUsingHigtFrame = true;
        //是否使用背景音乐
        public bool bUsingBgAudio = true;
        //背景音乐音量比例
        public float fBgAudioValue = 1f;
        //游戏音效音量比例
        public float fAudioValue = 1f;

        //大区Id
        public string iAreaId = "-1";
        //平台id
        public int iPlat = -1;
        
        public Dictionary<string, int> dServerIdDic = new Dictionary<string, int>();//上次登陆的ip对应id，<服务器IP,服务器ID>
        /// <summary>
        /// 最新客户端版本
        /// </summary>
        public string sLastClientVersion;
        //服务器时区
        public int serverTimeZone = 8;

        ////上次InjectFix的Tag标记
        //public string lastInjectFixVersion;
        //上次cdn的客户端版本号的Tag标记，新版本的后台，version只是同个版本号内唯一
        //public string lastCdnClientVersion;
        
        //上次ilruntime的tag标记
        //public string lastIlRuntimeVersion;
        //是否同意隐私策略协议
        public bool isAgreePolicy;
        //当前账号是否是白名单
        public bool isWhite;
        //是否使用屏幕点击效果
        public bool usingScreenClickEffect = true;
        //游戏画质
        public EGameViewQuality gameQuality = EGameViewQuality.NORMAL;
        //是否已发送激活埋点
        public bool isSendActivate;
    }
    
    public class GameSetting : _AALBasicSettingInfo
    {
        private static GameSetting _g_instance;
        public static GameSetting instance
        {
            get
            {
                if(null == _g_instance)
                    _g_instance = new GameSetting(_m_sPath);
                return _g_instance;
            }
        }

        private static string _m_sPath = "game_setting";
        
        private GameSettingData _m_settingData;

        public GameSetting(string _savePath)
           : base(_savePath)
        {
            _m_settingData = new GameSettingData();
        }
        
        /// <summary>
        /// 服务端时区
        /// </summary>
        public int serverTimeZone { get { return _m_settingData.serverTimeZone; } }
        /// <summary>
        /// 是否是白名单
        /// </summary>
        public bool isWhite { get { return _m_settingData.isWhite; } }

        /*************
        * 构建需要保存的字符串
         * 
        **/
        protected override string _makeSettingStr()
        {
            return JsonUtility.ToJson(_m_settingData);
        }

        /**************
       * 读取保存的字符串
       **/
        protected override void _initSettingStr(string _infoStr)
        {
            if(string.IsNullOrEmpty(_infoStr))
                return;

            _m_settingData = JsonUtility.FromJson<GameSettingData>(_infoStr);
            //设置游戏画质
            //QualityMgr.instance.setQualityLevel(_m_settingData.gameQuality);
        }

        /// <summary>
        /// 是否使用高帧率
        /// </summary>
        public bool usingHighFrame
        {
            get
            {
                if (null == _m_settingData)
                    return false;
                return _m_settingData.bUsingHigtFrame;
            }
            set
            {
                if (null == _m_settingData)
                    return;

                _m_settingData.bUsingHigtFrame = value;
                saveSetting();
            }
        }

        /// <summary>
        /// 是否使用屏幕点击效果
        /// </summary>
        public bool usingScreenClickEffect
        {
            get
            {
                if (null == _m_settingData)
                    return false;
                return _m_settingData.usingScreenClickEffect;
            }
            set
            {
                if (null == _m_settingData)
                    return;

                _m_settingData.usingScreenClickEffect = value;
                saveSetting();
            }
        }

        /// <summary>
        /// 游戏画质
        /// </summary>
        public EGameViewQuality gameQuality
        {
            get
            {
                if (null == _m_settingData)
                    return EGameViewQuality.NORMAL;
                return _m_settingData.gameQuality;
            }
            set
            {
                if (null == _m_settingData)
                    return;

                _m_settingData.gameQuality = value;
                //QualityMgr.instance.setQualityLevel(_m_settingData.gameQuality);
                saveSetting();
            }
        }

        /// <summary>
        /// 自动根据机器信息，设置Quality，仅在第一次进入的时候使用
        /// </summary>
        public void autoSetQuality()
        {
#if UNITY_IOS
            string modelStr = SystemInfo.deviceModel;
            string modelType = modelStr.ToLower().Trim().Substring(0, 3);
            if (modelType == "iph")
            {
                // iPhone:	                    iPhone1,1
                // iPhone 3G:	                iPhone1,2
                // iPhone 3GS:	                iPhone2,1
                // iPhone 4:	                iPhone3,1
                //                              iPhone3,2
                //                              iPhone3,3
                // iPhone 4S:	                iPhone4,1
                // iPhone 5:	                iPhone5,1
                //                              iPhone5,2
                // iPhone 5c:	                iPhone5,3
                //                              iPhone5,4
                // iPhone 5s:	                iPhone6,1
                //                              iPhone6,2
                // iPhone 6:	                iPhone7,2
                // iPhone 6 Plus:	            iPhone7,1
                // iPhone 6s:	                iPhone8,1
                // iPhone 6s Plus:	            iPhone8,2
                // iPhone SE (1st generation):	iPhone8,4
                // iPhone 7:	                iPhone9,1
                //                              iPhone9,3
                // iPhone 7 Plus:	            iPhone9,2
                //                              iPhone9,4
                // iPhone 8:	                iPhone10,1
                //                              iPhone10,4
                // iPhone 8 Plus:	            iPhone10,2
                //                              iPhone10,5
                // iPhone X:	                iPhone10,3
                //                              iPhone10,6
                // iPhone XR:	                iPhone11,8
                // iPhone XS:	                iPhone11,2
                // iPhone XS Max:	            iPhone11,6
                //                              iPhone11,4
                // iPhone 11:	                iPhone12,1
                // iPhone 11 Pro:	            iPhone12,3
                // iPhone 11 Pro Max:	        iPhone12,5
                // iPhone SE (2nd generation):	iPhone12,8
                // iPhone 12 mini:	            iPhone13,1
                // iPhone 12:	                iPhone13,2
                // iPhone 12 Pro:	            iPhone13,3
                // iPhone 12 Pro Max:	        iPhone13,4
                // iPhone 13 mini:	            iPhone14,4
                // iPhone 13:	                iPhone14,5
                // iPhone 13 Pro:	            iPhone14,2
                // iPhone 13 Pro Max:	        iPhone14,3
                // iPhone SE (3rd generation):	iPhone14,6
                // iPhone 14:	                iPhone14,7
                // iPhone 14 Plus:	            iPhone14,8
                // iPhone 14 Pro:	            iPhone15,2
                // iPhone 14 Pro Max:	        iPhone15,3

                if (modelStr.Equals("iPhone1,1") 
                    || modelStr.Equals("iPhone1,2")
                    || modelStr.Equals("iPhone2,1")
                    || modelStr.Equals("iPhone3,1")
                    || modelStr.Equals("iPhone3,2")
                    || modelStr.Equals("iPhone3,3")
                    || modelStr.Equals("iPhone4,1")
                    || modelStr.Equals("iPhone5,1")
                    || modelStr.Equals("iPhone5,2")
                    || modelStr.Equals("iPhone5,3")
                    || modelStr.Equals("iPhone5,4")
                    || modelStr.Equals("iPhone6,1")
                    || modelStr.Equals("iPhone6,2")
                    || modelStr.Equals("iPhone7,2")
                    || modelStr.Equals("iPhone7,1")
                    || modelStr.Equals("iPhone8,1")
                    || modelStr.Equals("iPhone8,2")
                    || modelStr.Equals("iPhone8,4")
                    || modelStr.Equals("iPhone9,1")
                    || modelStr.Equals("iPhone9,3")
                    || modelStr.Equals("iPhone9,2")
                    || modelStr.Equals("iPhone9,4")
                ) // iphone7 及以下都设置为Low
                    gameQuality = ENPGameQuality.LOW;
                // iphone8 、8Plus设置中
                else if (modelStr.Equals("iPhone10,1")
                    || modelStr.Equals("iPhone10,4")
                    || modelStr.Equals("iPhone10,2")
                    || modelStr.Equals("iPhone10,5"))
                    gameQuality = ENPGameQuality.NORMAL;
                //其他都设置为高
                else 
                    gameQuality = ENPGameQuality.HIGH; 
            }
            else if (modelType == "ipa")
            {
                //iPad机型
                gameQuality = ENPGameQuality.HIGH;
            }
#elif UNITY_ANDROID
            if (SystemInfo.graphicsShaderLevel <= 35 || SystemInfo.systemMemorySize < 2048)
                gameQuality = ENPGameQuality.LOW;
            else if (SystemInfo.systemMemorySize < 4096)
                gameQuality = ENPGameQuality.NORMAL;
            else
                gameQuality = ENPGameQuality.HIGH;
#else
            gameQuality = EGameViewQuality.HIGH;
#endif
        }

        /// <summary>
        /// 获取当前游戏语言
        /// </summary>
        /// <returns></returns>
        public EGameLanguage getCurrentLanguage()
        {
            if (null == _m_settingData)
                return EGameLanguage.EN_US;
            
            if(_m_settingData.eLang != EGameLanguage.NONE)
                return _m_settingData.eLang;
            return EGameLanguage.EN_US;
        }
        
        //设置当前语言
        public void setCurrentLanguage(EGameLanguage _lang)
        {
            if(null == _m_settingData || _m_settingData.eLang == _lang)
                return;

            _m_settingData.eLang = _lang;

            saveSetting();
        }
        
        /// <summary>
        /// 根据服务器ip获取上次登陆的服务器id
        /// </summary>
        /// <param name="_ip"></param>
        public int getServerIdByIp(string _ip)
        {
            int serverId = 0;
            if (_m_settingData == null ||  _m_settingData.dServerIdDic == null || string.IsNullOrEmpty(_ip))
            {
                if (_AALMonoMain.instance != null && _AALMonoMain.instance.showDebugOutput)
                    Debug.Log($"【{Time.frameCount}】[Setting] getServerIdByIp: 根据服务器ip获取上次登陆的服务器id，_m_settingData == null ||  _m_settingData.dServerIdDic == null || string.IsNullOrEmpty(_ip)，IP：{_ip}  ServerId:{0}");
                return 0;
            }

            if (_m_settingData.dServerIdDic.ContainsKey(_ip))
                serverId = _m_settingData.dServerIdDic[_ip];
            else
                serverId = 0;
            
            if (_AALMonoMain.instance != null && _AALMonoMain.instance.showDebugOutput)
                Debug.Log($"【{Time.frameCount}】[Setting] getServerIdByIp: 根据服务器ip获取上次登陆的服务器id，IP：{_ip}  ServerId:{serverId}");
            return serverId;
        }
        
        /// <summary>
        /// 设置服务器ID
        /// </summary>
        /// <param name="_ip"></param>
        /// <param name="_serverId"></param>
        public void setServerId(string _ip,int _serverId)
        {
            if(string.IsNullOrEmpty(_ip))
                return;
            if(null == _m_settingData)
                return;
        
            if(_m_settingData.dServerIdDic == null)
                _m_settingData.dServerIdDic = new Dictionary<string, int>();

            if(_m_settingData.dServerIdDic.ContainsKey(_ip))
                _m_settingData.dServerIdDic[_ip] = _serverId;
            else
                _m_settingData.dServerIdDic.Add(_ip, _serverId);

        }

        /// <summary>
        /// 设置是否使用单位高清资源
        /// </summary>
        /// <param name="_isFirstEnterGame"></param>
        public void setIsUsingLowActorClarity(bool _isUsingLowActorClarity)
        {
            if(null == _m_settingData || _m_settingData.bUsingLowActorClarity == _isUsingLowActorClarity)
                return;

            _m_settingData.bUsingLowActorClarity = _isUsingLowActorClarity;

            saveSetting();
        }
        
        /// <summary>
        /// 获取是否使用单位高清资源
        /// </summary>
        /// <returns></returns>
        public bool usingLowActorClarity
        {
            get
            {
                if (null == _m_settingData)
                    return false;
            
                return _m_settingData.bUsingLowActorClarity;
            }
        }
        
        /// <summary>
        /// 设置是否第一次进入游戏
        /// </summary>
        /// <param name="_isFirstEnterGame"></param>
        public void setIsFirstEnterGame(bool _isFirstEnterGame)
        {
            if(null == _m_settingData || _m_settingData.bIsFirstEnterGame == _isFirstEnterGame)
                return;

            _m_settingData.bIsFirstEnterGame = _isFirstEnterGame;

            saveSetting();
        }
        
        /// <summary>
        /// 取是否第一次进入游戏
        /// </summary>
        /// <returns></returns>
        public bool IsFirstEnterGame()
        {
            if (null == _m_settingData)
                return false;
            
            return _m_settingData.bIsFirstEnterGame;
        }

        /// <summary>
        /// 是否使用背景音乐
        /// </summary>
        /// <returns></returns>
        public bool usingBgAudio
        {
            get
            {
                if (null == _m_settingData)
                    return false;
            
                return _m_settingData.bUsingBgAudio;
            }
        }
        
        /// <summary>
        /// 是否使用音效
        /// </summary>
        /// <returns></returns>
        public bool usingAudio
        {
            get
            {
                if (null == _m_settingData)
                    return false;
            
                return _m_settingData.bUsingAudio;
            }
        }
        
        /// <summary>
        /// 背景音乐音量比例
        /// </summary>
        /// <returns></returns>
        public float bgAudioValue
        {
            get
            {
                if (null == _m_settingData)
                    return 0f;
            
                return _m_settingData.fBgAudioValue;
            }
        }
        
        /// <summary>
        /// 游戏音效音量比例
        /// </summary>
        /// <returns></returns>
        public float audioValue
        {
            get
            {
                if (null == _m_settingData)
                    return 0f;
            
                return _m_settingData.fAudioValue;
            }
        }
       
        
        /// <summary>
        /// 设置音效相关参数
        /// </summary>
        public void setAudioSettingAfterLoadData()
        {
            if(null == _m_settingData)
                return;
            
            //PlayAudioMgr.instance.chgAudioValue(_m_settingData.bUsingAudio, _m_settingData.fAudioValue);
            //BattlePlayAudioMgr.instance.chgAudioValue(_m_settingData.bUsingAudio, _m_settingData.fAudioValue);
            //PlayAudioMgr.instance.chgBgAudioValue(_m_settingData.bUsingBgAudio, _m_settingData.fBgAudioValue);
            //BattlePlayAudioMgr.instance.chgBgAudioValue(_m_settingData.bUsingBgAudio, _m_settingData.fBgAudioValue);
        }

        /// <summary>
        /// 设置是否使用音效
        /// </summary>
        /// <param name="_isUsing"></param>
        public void setIsUsingAudio(bool _isUsing)
        {
            if (null == _m_settingData)
                return;
            _m_settingData.bUsingAudio = _isUsing;
            setAudioSettingAfterLoadData();
            saveSetting();
        }

        /// <summary>
        /// 设置音效音量
        /// </summary>
        /// <param name="_value"></param>
        public void setAudioValue(float _value)
        {
            if (null == _m_settingData)
                return;
            _m_settingData.fAudioValue = _value;
            setAudioSettingAfterLoadData();
            saveSetting();
        }

        /// <summary>
        /// 设置是否使用背景音乐
        /// </summary>
        /// <param name="_isUsing"></param>
        public void setIsUsingBgAudio(bool _isUsing)
        {
            if (null == _m_settingData)
                return;
            _m_settingData.bUsingBgAudio = _isUsing;
            setAudioSettingAfterLoadData();
            saveSetting();
        }

        /// <summary>
        /// 设置背景音乐音量
        /// </summary>
        /// <param name="_value"></param>
        public void setBgAudioValue(float _value)
        {
            if (null == _m_settingData)
                return;
            _m_settingData.fBgAudioValue = _value;
            setAudioSettingAfterLoadData();
            saveSetting();
        }
        
        public void setAreaId(string _areaId, int _plat)
        {
            if(null == _m_settingData)
                return;
            _m_settingData.iAreaId = _areaId;
            _m_settingData.iPlat = _plat;
        }
        
        /// <summary>
        /// 大区Id
        /// </summary>
        /// <returns></returns>
        public string getAreaId()
        {
            if (null == _m_settingData)
                return "-1";
            return _m_settingData.iAreaId;
        }

        public void setLastClientVersion(string _lastClientVersion)
        {
            if (null == _m_settingData)
                return ;
            _m_settingData.sLastClientVersion = _lastClientVersion;
        }


        
        /// <summary>
        /// 设置上次InjectFix的Tag标记
        /// </summary>
        /// <param name="_isSelectedMulitPour"></param>
        public void setLastInjectFixVersion(string _lastInjectFixVersion)
        {
            if(null == _m_settingData)
                return;
            //_m_settingData.lastInjectFixVersion = _lastInjectFixVersion;
            saveSetting();
        }
        
        /// <summary>
        /// 上次InjectFix的Tag标记
        /// </summary>
        /// <returns></returns>
        public string getLastInjectFixVersion()
        {
            return string.Empty;
            //if (null == _m_settingData)
            //    return string.Empty;
            //return _m_settingData.lastInjectFixVersion;
        }
        
        /// <summary>
        /// 设置上次IlRuntime的Tag标记
        /// </summary>
        /// <param name="_isSelectedMulitPour"></param>
        public void setLastIlRuntimeVersion(string _lastIlRuntimeVersion)
        {
            if(null == _m_settingData)
                return;
            //_m_settingData.lastIlRuntimeVersion = _lastIlRuntimeVersion;
            saveSetting();
        }
        
        /// <summary>
        /// 上次IlRuntime的Tag标记
        /// </summary>
        /// <returns></returns>
        public string getLastIlRuntimeVersion()
        {
            return string.Empty;
            //if (null == _m_settingData)
            //    return string.Empty;
            //return _m_settingData.lastIlRuntimeVersion;
        }

        /// <summary>
        /// 设置是否同意隐私协议
        /// </summary>
        /// <param name="_isAgreePolicy"></param>
        public void setIsAgreePolicy(bool _isAgreePolicy)
        {
            if (null == _m_settingData)
                return;
            _m_settingData.isAgreePolicy = _isAgreePolicy;
            saveSetting();
        }

        /// <summary>
        /// 是否同意隐私协议
        /// </summary>
        /// <returns></returns>
        public bool isAgreePolicy()
        {
            if (null == _m_settingData)
                return false;
            return _m_settingData.isAgreePolicy;
        }

        /// <summary>
        /// 设置是否是白名单
        /// </summary>
        /// <param name="_isWhite"></param>
        public void setIsWhite(bool _isWhite)
        {
            if (null == _m_settingData)
                return;

            _m_settingData.isWhite = _isWhite;
            saveSetting();
        }

        /// <summary>
        /// 设置服务器时区
        /// </summary>
        /// <param name="_serverTimeZone"></param>
        public void setServerTimeZone(int _serverTimeZone)
        {
            if (null == _m_settingData)
                return;

            _m_settingData.serverTimeZone = _serverTimeZone;
            saveSetting();
        }
        
        /// <summary>
        /// 设置上次cdn的客户端版本号的Tag标记，新版本的后台，version只是同个版本号内唯一
        /// </summary>
        /// <param name="_isSelectedMulitPour"></param>
        public void setLastCdnClientVersion(string _lastCdnClientVersion)
        {
            if(null == _m_settingData)
                return;
            //_m_settingData.lastCdnClientVersion = _lastCdnClientVersion;
            saveSetting();
        }
        
        /// <summary>
        /// 上次cdn的客户端版本号的Tag标记，新版本的后台，version只是同个版本号内唯一
        /// </summary>
        /// <returns></returns>
        public string getLastCdnClientVersion()
        {
            return string.Empty;

            //if (null == _m_settingData)
            //    return string.Empty;
            //return _m_settingData.lastCdnClientVersion;
        }

        /// <summary>
        /// 设置是否发送激活埋点
        /// </summary>
        /// <param name="_isSend"></param>
        public void setIsSendActivate(bool _isSend)
        {
            if(null == _m_settingData)
                return;
            _m_settingData.isSendActivate = _isSend;
            saveSetting();
        }

        /// <summary>
        /// 是否发送激活埋点
        /// </summary>
        /// <returns></returns>
        public bool getIsSendActivate()
        {
            if (null == _m_settingData)
                return false;

            return _m_settingData.isSendActivate;
        }
    }
}